package com.util;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONObject;

public class CacheUtil {

	public static String getKey(Map<String, String> params)
			throws UnsupportedEncodingException {
		Set<String> keysSet = params.keySet();
		Object[] keys = keysSet.toArray();
		Arrays.sort(keys);
		JSONObject jsonObject=new JSONObject();
		for (Object key : keys) {
			Object value = params.get(key);
			String valueString = "";
			if (null != value) {
				valueString = String.valueOf(value);
			}
			jsonObject.put(key, valueString);
		}
		return  jsonObject.toString();
	}

	public static Map<String, String> getParams(HttpServletRequest request) {
		Enumeration<?> pNames = request.getParameterNames();
		Map<String, String> params = new HashMap<String, String>();
		String uri=request.getRequestURI();
		params.put("uri", uri);
		while (pNames.hasMoreElements()) {
			String pName = (String) pNames.nextElement();
			if("signToken".equals(pName))continue;
			if("deviceId".equals(pName))continue;
			if("deviceType".equals(pName)) continue;
			if("mId".equals(pName)) continue;
			String pValue = request.getParameter(pName);
			params.put(pName, pValue);
		}
		String curPge=params.get("curPage");
		if(StringUtils.isEmpty(curPge))
			params.put("curPage", String.valueOf(0));
		String pageSize=params.get("pageSize");
		if(StringUtils.isEmpty(pageSize))
			params.put("pageSize", String.valueOf(10));
		
		return params;
	}
	
	public static String getValue(Map<String, Object> values){
		values.put("timestamp", System.currentTimeMillis());
		JSONObject jsonObject=JSONObject.fromObject(values);
		return jsonObject.toString();
	}
}
